

/*
program to plot preferred habitat regression output

plot_ph_alt_regression
inputs: 
- fname_stub: stub name of dta files with low/high results

optional:
- plotname_stub: stub for naming figures (default=fname_stub)
- show_figs: whether to display figures
- figname: save figures after combining

outputs:
- memory graphs of rolling results, and p-values


*/


********************************************************************************
********************************************************************************
cap program drop plot_ph_alt_regression 
program define plot_ph_alt_regression

* parameter arguments, maturity, and forward/zero rate variable names
syntax , fname_stub(string) ///
	[plotname_stub(string) show_figs(real 0) combine_figs(real 1) ///
	plot_pvals(real 1) figname(string) ]

***********************************
* load estimation results
preserve
use ../data/rolling/phaltreg_`fname_stub'.dta, clear
* endpoint of maturity window as x axis
gen x = maturity_end

* create confidence intervals
foreach coeff in B_shock_S0 B_shock_S1 B_shock_L0 B_shock_L1 {
	gen cil_`coeff' = `coeff' - 2*sqrt(V_`coeff'_`coeff')
	gen ciu_`coeff' = `coeff' + 2*sqrt(V_`coeff'_`coeff')
}


* for naming figure
if "`plotname_stub'"=="" {
    local plotname_stub "`fname_stub'"
}
* plots
forvalues i=0/1 {
	* if separate figs: label both, no title
	if `combine_figs'==0 {
		local x_axis_str xtitle("Maturity")
		local title_str title("")
		local legend_str legend(order(2 "Short-Term Auctions" ///
			3 "Long-Term Auctions") ring(0) position(11) rows(2) )
	}
	* if combining: xlabel only bottom fig, label only top, title both
	else {
		if `i'==0 {
			local x_axis_str xtitle("")
			local title_str title("Panel A: Non-Crisis")
			local legend_str legend(order(2 "Short-Term Auctions" ///
				3 "Long-Term Auctions") ring(0) position(11) rows(2) )
		}
		else {
			local x_axis_str xtitle("Maturity")
			local title_str title("Panel B: Crisis")
			local legend_str legend(off)
		}
	}
	* whether to show figures in window
	if `show_figs'==0 local nodraw "nodraw"
	else local nodraw ""
	
	* coeff plot
	twoway ///
		(rarea ciu_B_shock_S`i' cil_B_shock_S`i' x, color(gs12)) ///
		(line B_shock_S`i' x, ///
			lcolor("scheme p1") lpattern(solid)) ///
		(line B_shock_L`i' ciu_B_shock_L`i' cil_B_shock_L`i' x, ///
			lcolor("scheme p2" "scheme p2" "scheme p2") ///
			lwidth(medium thin thin) ///
			lpattern(solid shortdash shortdash ) ) ///
		, ytitle("Coefficient") /// 
			`title_str' `legend_str' `x_axis_str' ///
			name(`plotname_stub'`i', replace) `nodraw'
	
	if `plot_pvals'==1 {
		* p-val plot with 5pct line
		twoway bar eq_pval`i' x, barwidth(.5) color(gs12) ///
			`title_str' ytitle("P-value") `x_axis_str' ///
			yline(0.05, lcolor(black) lwidth(0.25) lpattern(dash) ) ///
			name(`plotname_stub'`i'_pvals, replace) `nodraw'
	}

}
if "`figname'"!="" {
	* optinally combine (vertically) and export
	if `combine_figs'==1 {
		* coeffs
		graph combine `plotname_stub'0 `plotname_stub'1, ///
			rows(2) ycommon imargin(small) ///
			xsize(5) ysize(7) name(`plotname_stub', replace)
		graph export ../figs/`figname'.pdf, replace
		graph export ../figs/`figname'.eps, replace

		* pvals
		if `plot_pvals'==1 {
			graph combine `plotname_stub'0_pvals `plotname_stub'1_pvals, ///
				rows(2) imargin(small) ///
				xsize(5) ysize(7) name(`plotname_stub'_pvals, replace) 
			graph export ../figs/`figname'_pvals.pdf, replace
			graph export ../figs/`figname'_pvals.eps, replace
		}
	}
	else {
		forvalues i=0/1 {
			* coeffs
			graph display `plotname_stub'`i', xsize(8) ysize(5)
			graph export ../figs/`figname'`i'.pdf, replace
			graph export ../figs/`figname'`i'.eps, replace
			
			* p-vals
			if `plot_pvals'==1 {
				graph display `plotname_stub'`i'_pvals, xsize(8) ysize(5)
				graph export ../figs/`figname'`i'_pvals.pdf, replace
				graph export ../figs/`figname'`i'_pvals.eps, replace
			}
			
		}
	}
}


end


